//
//  JWFindBusinessVC.m
//  jinwang
//
//  Created by jzb on 2018/4/12.
//  Copyright © 2018年 jzb. All rights reserved.
//

#import "JWFindBusinessVC.h"
#import "JWFBFacadeVCModel.h"
#import "JWFBFacadeTagVC.h"
#import "JWFindBusinessAddTagVC.h"

#import "JWFindBussinessReleaseVC.h"
#import "JWFindBusinessTagVC.h"

#import "JWFindBusinessMyFaBuVC.h"

@interface JWFindBusinessVC ()

//定位
@property (nonatomic,strong) UIButton *addressBtn;
//发布
@property (nonatomic,strong) UIButton *fabuBtn;

@property (nonatomic, strong) MLMSegmentHead *segHead;
@property (nonatomic, strong) MLMSegmentScroll *segScroll;

@property (nonatomic, strong) NSMutableArray *ListmodelArr,*list;


@end

@implementation JWFindBusinessVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"快急商务";
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.f) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    self.view.backgroundColor = [UIColor whiteColor];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"back_common"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(goBack)];
    UIBarButtonItem *photoItem = [[UIBarButtonItem alloc]initWithCustomView:self.addressBtn];
    
    self.navigationItem.leftBarButtonItems = @[backItem,photoItem];
    
    UIBarButtonItem *shaixuanItem = [[UIBarButtonItem alloc]initWithCustomView:self.fabuBtn];
    self.navigationItem.rightBarButtonItem = shaixuanItem;
    
    
//    NSArray *arr = [kUserDefaults objectForKey:@"BusinessArr"];
//
//    if (arr.count > 0) {
//
//        [self segHeadViewBuild];
//    }else{
    
         [self makeUrl_Business_defaultTag];
//    }
   
}
- (void)segHeadViewBuild{
    
    _list = [NSMutableArray new];
    
    for (int i = 0; i < _ListmodelArr.count; i++) {
        
        JWFBFacadeVCModel *model = _ListmodelArr[i];
        
        [_list addObject:model.tag_name];
    }
    _segHead = [[MLMSegmentHead alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth-60, 44) titles:_list headStyle:SegmentHeadStyleLine layoutStyle:MLMSegmentLayoutLeft];
    //    _segHead.fontScale = 1.1;
    _segHead.fontSize = 17;
    _segHead.deSelectColor = kRGBColor(80, 80, 80);
    _segHead.selectColor = UIColorHex(0xEEA72B);
    _segHead.lineScale = .8;
    _segHead.bottomLineHeight = .5;
    _segHead.lineColor = UIColorHex(0xEEA72B);
    _segHead.bottomLineColor = kRGBColor(230, 230, 230);
    //    _segHead.moreButton_width = 60;
    
    UIButton *moreBtn = [[UIButton alloc]initWithFrame:CGRectMake(kScreenWidth-45, 4, 30, 30)];
    [moreBtn setImage:[UIImage imageNamed:@"jw_find_shop_blackAddIcom"] forState:UIControlStateNormal];
    [moreBtn addTarget:self action:@selector(moreBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:moreBtn];
    UIView *lineVeiw = [[UIView alloc]initWithFrame:CGRectMake(kScreenWidth - 60, 43.5, 60, .5)];
    lineVeiw.backgroundColor = kRGBColor(230, 230, 230);
    [self.view addSubview:lineVeiw];
    
    _segScroll = [[MLMSegmentScroll alloc] initWithFrame:CGRectMake(0, 44, kScreenWidth, kScreenHeight-44-50-64) vcOrViews:[self vcArr:_list.count]];
    
    _segScroll.loadAll = YES;
    _segScroll.showIndex = 0;
    
    _segScroll.backgroundColor = [UIColor purpleColor];
    
    [MLMSegmentManager associateHead:_segHead withScroll:_segScroll completion:^{
        
        [self.view addSubview:_segHead];
        [self.view addSubview:_segScroll];
    }];
    
    UIButton *myFaBuBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, kScreenHeight-50-64, kScreenWidth, 50)];
    myFaBuBtn.backgroundColor = UIColorHex(0xEEA72B);
    [myFaBuBtn setTitle:@"我的发布" forState:UIControlStateNormal];
    [myFaBuBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [myFaBuBtn addTarget:self action:@selector(MyFaBuBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
    myFaBuBtn.titleLabel.font = [UIFont systemFontOfSize:17];
    [self.view addSubview:myFaBuBtn];
}
#pragma mark - 数据源
- (NSArray *)vcArr:(NSInteger)count {
    NSMutableArray *arr = [NSMutableArray array];
    for (int i = 0; i < _ListmodelArr.count; i++) {
        JWFBFacadeVCModel *model = _ListmodelArr[i];
        JWFindBusinessTagVC *FacadeTagVC = [[JWFindBusinessTagVC alloc]init];
        FacadeTagVC.tag_id = [model.tag_id stringValue];
        //        FacadeTagVC.parent_id = _parent_id;
        FacadeTagVC.view.backgroundColor = [UIColor cyanColor];
        [arr addObject:FacadeTagVC];
    }
    return arr;
}
- (void)moreBtnOnclick:(UIButton *)sender{
    
    JWFindBusinessAddTagVC *vc = [JWFindBusinessAddTagVC new];
    vc.title = @"快急商务";
    vc.ListmodelArr = _ListmodelArr;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)MyFaBuBtnOnclick:(UIButton *)sender{
    JWFindBusinessMyFaBuVC *vc = [JWFindBusinessMyFaBuVC new];
   
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark -网络请求
//获取默认标签
-(void)makeUrl_Business_defaultTag{
    [[DDNetworkManagerDate makeUrl_Business_defaultTagWithtoken:k_user_token] post_RequestFinshSuccess:^(id responseObject) {
        
        NSLog(@"-=-=>>%@",responseObject);
        if ([self requestFinshCode:responseObject isShowMessage:NO] == 1) {
            _ListmodelArr = [NSMutableArray new];
            NSArray *data = [responseObject objectForKey:kRequsetDataKey];
            //转模型
            NSMutableArray *arr = [NSMutableArray array];
            [data enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                JWFBFacadeVCModel *model = [JWFBFacadeVCModel yy_modelWithDictionary:obj];
                [arr addObject:model];
            }];
            self.ListmodelArr = arr.mutableCopy;
            
            if (_ListmodelArr.count > 0) {
//                [kUserDefaults setObject:_ListmodelArr forKey:@"BusinessArr"];
//                [kUserDefaults synchronize];
                
                [self segHeadViewBuild];
            }
            
        }
        
        
    } failure:^(id errorObject) {
        
        [self showBottomMessag:@"网络请求失败"];
    }];
}

#pragma mark -懒加载
- (UIButton *)addressBtn{
    if (!_addressBtn) {
        _addressBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _addressBtn.frame = CGRectMake(0, 0, 44, 44);
        [_addressBtn setImage:[UIImage imageNamed:@"jw_find_dingwei"] forState:UIControlStateNormal];
        [_addressBtn addTarget:self action:@selector(addressBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addressBtn;
}
#pragma mark -懒加载
- (UIButton *)fabuBtn{
    if (!_fabuBtn) {
        _fabuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _fabuBtn.frame = CGRectMake(0, 0, 44, 44);
        [_fabuBtn setTitle:@"发布" forState:UIControlStateNormal];
        [_fabuBtn addTarget:self action:@selector(fabuBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _fabuBtn;
}
- (void)addressBtnOnclick:(UIButton *)sender{
    
    
}
- (void)fabuBtnOnclick:(UIButton *)sender{
    
    JWFindBussinessReleaseVC *VC = [JWFindBussinessReleaseVC new];
    
    [self.navigationController pushViewController:VC animated:YES];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
